﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Xml.Linq;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;
using WebApp.Domain;
using WebApp.Data;
using WebApp.Service;
using System.Data.Services;
using COM.Data;
using COM.Web;

namespace WebApp.Web
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class WcfTempFileService : WcfServiceBase, IWcfTempFileService
    {
        protected ITempFileService tempFileService = COM.Web.Unity.Instance.GetService<ITempFileService>();
        protected IUserPrincipalFactory userPrincipalFactory = COM.Web.Unity.Instance.GetService<IUserPrincipalFactory>();

        [WebInvoke]
        public string CreateTempFile(Guid fileItemID, string ip, Guid userID, string userAgent)
        {
            System.Web.HttpContext.Current.User = this.userPrincipalFactory.CreateMvcUserPrincipal(userID, "Anonymous");

            var tempFile = new TempFile()
            {
                ID = Guid.NewGuid(),
                FileItemID = fileItemID,
                Ip = ip,
                IsExpiredAfterDownload = false,
                WillExpiredDate = DateTime.Now.AddDays(7),
                UserID = userID,
                UserAgent = userAgent,
            };

            var newCodeService = Unity.Instance.GetService<INewCodeService>();
            tempFile.ServiceCode = newCodeService.NewRndReadString(6);
            this.tempFileService.Create(tempFile);
            return tempFile.TempCode;
        }

        [WebInvoke]
        public FileItem GetFileItemByID(Guid userID, Guid fileItemID)
        {
            //Todo:应该登录,Cookie中就有一个值
            System.Web.HttpContext.Current.User = this.userPrincipalFactory.CreateMvcUserPrincipal(userID, "Anonymous");

            var fileItemService = Unity.Instance.GetService<IFileItemService>();
            var fileItem = fileItemService.GetFileItemID(fileItemID);
            return fileItem;
        }

        [WebInvoke]
        public TempFile GetTempFileByCode(Guid userID, string tempCode)
        {
            //Todo:应该登录,Cookie中就有一个值
            System.Web.HttpContext.Current.User = this.userPrincipalFactory.CreateMvcUserPrincipal(userID, "Anonymous");
            var tempFile = this.tempFileService.GetNotExpiredTempFileByTempCode(tempCode);
            return tempFile;
        }


        [WebInvoke]
        public void UpdateTempFile(Guid userID, Guid tempFileID, int willExpiredAfterDays, bool isExpiredAfterDownload, bool isExpiredNow)
        {
            //Todo:应该登录,Cookie中就有一个值
            System.Web.HttpContext.Current.User = this.userPrincipalFactory.CreateMvcUserPrincipal(userID, "Anonymous");

            var tempFile = this.tempFileService.GetTempFileByID(tempFileID);
            if(isExpiredNow)
            {
                this.tempFileService.SetExpiredNow(tempFileID);
            }
            else
            {
                tempFile.IsExpiredAfterDownload = isExpiredAfterDownload;

                var existsDays = (tempFile.WillExpiredDate.Date - DateTime.Now.Date).Days;
                if (willExpiredAfterDays != existsDays)
                {
                    if (willExpiredAfterDays < 0)
                        willExpiredAfterDays = 0;
                    tempFile.WillExpiredDate = DateTime.Now.AddDays(willExpiredAfterDays);
                }
                this.tempFileService.Update(tempFile);
            }

        }
    }
}
